import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';
import "package:get/get.dart";
import 'package:pull_to_refresh/pull_to_refresh.dart';

import '../../common/my_icon.dart';
import '../../components/appbar_widget/appbar_widget.dart';
import '../../components/loading/svg_loading.dart';
import '../../components/status/empty.dart';
import '../../components/status/load_more_state.dart';
import '../../components/status/not_network.dart';
import '../../config/global_config.dart';
import '../../controller/network/network_ctrl.dart';
import '../../utils/screenutil/fit.dart';
import 'components/notice_list.dart';
import 'ctrl/notice_ctrl.dart';

class Notice extends StatelessWidget {
  static const _READ = "已读";
  static const _UNREAD = "未读";

  // 导入控制器
  final noticeCtrl = Get.put(NoticeMyCtrl());
  final _nc = Get.find<NetworkCtrl>();

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBarWidget("消息").build(context),
      //noticeCtrl.isInitComplete.value
      body: Obx(
        () => noticeCtrl.isInitComplete.value
            ? Container(
                height: double.infinity,
                child: Stack(
                  children: [
                    Container(
                      margin: EdgeInsets.only(
                        top: fit(_nc.topDistance.value + 87),
                      ),
                      child: noticeCtrl.listData.length > 0
                          ? SmartRefresher(
                              footer: CustomFooter(
                                builder: (BuildContext ct, LoadStatus mode) {
                                  Widget body = LoadMoreState(mode);
                                  return Container(child: Center(child: body));
                                },
                              ),
                              enablePullDown: false,
                              enablePullUp: true,
                              controller: noticeCtrl.refreshController.value,
                              onLoading: noticeCtrl.onLoading,
                              child: ListView.builder(
                                itemCount: noticeCtrl.listData.value.length,
                                itemBuilder: (BuildContext c, int i) => InkWell(
                                  onTap: () {
                                    // TODO: 已读信息是否需要弹窗提示??? 暂定为不弹窗*
                                    // TODO: 2022年1月21日 （注）
                                    if (noticeCtrl.status.value != 1) {
                                      noticeCtrl.seeUnreadNotice(
                                        "${NoticeList.noticeType[noticeCtrl.listData.value[i].type]['msg']}",
                                        "${noticeCtrl.listData.value[i].content}",
                                        id: noticeCtrl.listData.value[i].id,
                                        sign: i,
                                      );
                                    }
                                  },
                                  child: noticeCtrl.listData.value[i] != null
                                      ? NoticeList(
                                          noticeCtrl.listData.value[i],
                                        )
                                      : SizedBox.shrink(),
                                ),
                              ),
                            )
                          : Container(
                              width: 640.f,
                              height: double.infinity,
                              child: Center(child: Empty()),
                            ),
                    ),

                    // 选项卡
                    Positioned(
                      top: 0,
                      child: Container(
                        width: 640.f,
                        height: 85.f,
                        margin:
                            EdgeInsets.only(top: fit(_nc.topDistance.value)),
                        decoration: BoxDecoration(
                          color: Colors.white,
                          boxShadow: GlobalConfig.boxShadow,
                        ),
                        child: Flex(
                          direction: Axis.horizontal,
                          children: [
                            Expanded(
                              flex: 1,
                              child: createTabs(_UNREAD, 0),
                            ),
                            SizedBox(
                              width: 1.f,
                              child: Container(
                                height: 60.f,
                                color: GlobalConfig.borderColor,
                              ),
                            ),
                            Expanded(
                              flex: 1,
                              child: createTabs(_READ, 1),
                            ),
                          ],
                        ),
                      ),
                    ),

                    // 网络状态
                    Positioned(top: 0, child: NotNetwork()),
                  ],
                ),
              )
            : SVGLoading(),
      ),
      floatingActionButton: Obx(
        () => _nc.networkStatus.value && noticeCtrl.status.value != 1
            ? Container(
                width: 80.f,
                height: 80.f,
                decoration: BoxDecoration(
                  color: GlobalConfig.theme,
                  borderRadius: BorderRadius.circular(40.f),
                  boxShadow: GlobalConfig.boxShadow,
                ),
                margin: EdgeInsets.only(bottom: 20),
                child: IconButton(
                  onPressed: () => noticeCtrl.setAllReadNotice(),
                  icon: Icon(MyIcon.saob, color: Colors.white, size: 45.f),
                ),
              )
            : SizedBox.shrink(),
      ),
    );
  }

  /// 创建头部选项卡
  /// * [name] String 选项卡名称|必填
  /// * [currentStatus] bool 当前状态|必填
  Widget createTabs(String name, int currentStatus) {
    return InkWell(
      onTap: () => noticeCtrl.onChangeSelectStatus(currentStatus),
      child: Container(
        alignment: Alignment.center,
        child: Column(
          mainAxisAlignment: MainAxisAlignment.center,
          children: [
            Container(
              width: 200.f,
              height: 85.f,
              alignment: Alignment.center,
              decoration: BoxDecoration(
                border: Border(
                  bottom: BorderSide(
                    width: 2,
                    color: noticeCtrl.status.value != currentStatus
                        ? Colors.white
                        : GlobalConfig.theme,
                  ),
                ),
              ),
              child: Text(
                "$name",
                style: TextStyle(
                  fontSize: 28.f,
                  color: noticeCtrl.status.value != currentStatus
                      ? GlobalConfig.assistColor
                      : GlobalConfig.theme,
                ),
              ),
            ),
          ],
        ),
      ),
    );
  }
}
